package org.shj.basic.extendclass

object FuncDemo {
  def main(args: Array[String]): Unit = {
    combineFuncDemo()
    partialFuncDemo()
  }
  
  def combineFuncDemo(){
    def f(s:String)= "f("+s+")"
    def g(s:String)="g("+s+")"
    
    val fCombineG = f _ compose g _  // f(g(msg))
    println(fCombineG("a"))
    
    val fAndThenG = f _ andThen g _  // g(f(msg))
    println(fAndThenG("a"))
  }
  
  def partialFuncDemo(){
    val one: PartialFunction[Int, String] = {case 1 => "one"}
    println(one.isDefinedAt(1) + " : " + one(1))
    println(one.isDefinedAt(2))
    
    val two: PartialFunction[Int, String] = {case 2 => "two"}
    val three: PartialFunction[Int, String] = {case 3 => "three"}
    val wild: PartialFunction[Int, String] = {case _ => "something else"}
    
    val partial = one orElse two orElse three orElse wild
    println(partial(2))
    println(partial(3))
    println(partial(6))
  }
}